package com.example.config;

import com.alibaba.nacos.api.utils.StringUtils;
import io.jsonwebtoken.*;
import org.bouncycastle.math.ec.rfc8032.Ed25519;
import org.omg.CORBA.UnknownUserException;

import java.security.Key;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class JwtUtil {
      private static   final Integer EXPIRETIME=24*60*60;
      private static  final String SECRET="xiaoheizi!@!#!#!#!#!$!%!^%$!%$!#!#!#$!%";
    public static String getToken(String id,String name){
     Calendar instance = Calendar.getInstance();
         instance.set(Calendar.SECOND,EXPIRETIME);
        Map<String, Object> map = new HashMap<>();
       return Jwts.builder().setHeader(map)
                .claim("id",id)
                .claim("name",name)
                .setExpiration(instance.getTime())
                .setSubject("subject")
                .signWith(SignatureAlgorithm.HS512,SECRET)
                .compact();
    }
    public static Boolean verityToken(String token){
       if(StringUtils.isEmpty(token)){
          return false;
        }
         try {
             Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token);
         }catch (Exception e){
               return false;
         }
         return true;
    }
    public static Map<String,Object> tokenInfo(String token){
        Claims body = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();
        String id =(String) body.get("id");
        String name = (String) body.get("name");
        Map<String, Object> map=new HashMap<>();
        map.put("id",id);
        map.put("name",name);
        return map;
    }
}
